package cn.kshost.zqyg.manager.controller;

import cn.kshost.zqyg.manager.service.SysUserService;
import cn.kshost.zqyg.model.dto.system.AssginRoleDto;
import cn.kshost.zqyg.model.dto.system.SysRoleDto;
import cn.kshost.zqyg.model.dto.system.SysUserDto;
import cn.kshost.zqyg.model.entity.system.SysRole;
import cn.kshost.zqyg.model.entity.system.SysUser;
import cn.kshost.zqyg.model.vo.common.Result;
import cn.kshost.zqyg.model.vo.common.ResultCodeEnum;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    SysUserService sysUserService;

    @Operation(summary = "分页查询用户")
    @GetMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysUser>> findByPage(@PathVariable("pageNum") Integer pageNum,
                                                @PathVariable("pageSize") Integer pageSize,
                                                SysUserDto sysUserDto
                                                ){


        PageInfo<SysUser>  sysUserPageInfo =  sysUserService.findByPage(pageNum,pageSize,sysUserDto);
        return Result.build(sysUserPageInfo,ResultCodeEnum.SUCCESS );

    }
    @Operation(summary = "注册用户")
    @PostMapping("/addSysUser")
    public Result<SysUser> addSysUser(@RequestBody SysUser sysUser){
        //密码加密
        String password = DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes());
        sysUser.setPassword(password);
        //设置状态
        sysUser.setStatus(0);

        sysUserService.addSysUser(sysUser);

        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "更新用户")
    @PutMapping("/modifySysUser")
    public Result modifySysUser(@RequestBody SysUser sysUser){

        sysUserService.modifySysSUser(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/deleteSysUser")
    public Result deleteSysUser(@RequestBody SysUser sysUser){
        sysUserService.deleteSysUser(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }



}
